package com.bjsxt.shop.users.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.bjsxt.common.util.ConstatFinalUtil;
import com.bjsxt.common.util.PageInfoUtil;
import com.bjsxt.shop.users.dao.IAUsersAddressDao;
import com.bjsxt.shop.users.dao.IAUsersDao;
import com.bjsxt.shop.users.pojo.AUsers;
import com.bjsxt.shop.users.pojo.AUsersAddress;
import com.bjsxt.shop.users.service.IUsersShopService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

/**
 * 用户Service的实现类
 * 
 * @author WangshSxt
 *
 */
@Service("usersShopService")
public class UsersShopServiceImpl implements IUsersShopService
{
	@Resource
	private IAUsersDao usersDao;
	@Resource
	private IAUsersAddressDao usersAddressDao;
	
	@Override
	public AUsersAddress findOneUsersAddressService(Map<String, Object> condMap)
	{
		return this.usersAddressDao.findOne(condMap);
	}
	
	@Override
	public JSONObject saveOneUsersAddressService(AUsersAddress usersAddress)
	{
		JSONObject resultJSON = new JSONObject();
		/* 影响条数 */
		int res = this.usersAddressDao.save(usersAddress);
		/*String str = null ; 
		str.toString();*/
		if(res > 0 )
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
			
			/* 存储data */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", usersAddress.getId() + "");
			dataJSON.put("effect", res + "");
			
			resultJSON.put("data", dataJSON);
		}else
		{
			resultJSON.put("code", "1");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("1"));
		}
		return resultJSON;
	}
	
	@Override
	public JSONObject updateOneUsersAddressService(AUsersAddress usersAddress)
	{
		JSONObject resultJSON = new JSONObject();
		/* 影响条数 */
		int res = this.usersAddressDao.update(usersAddress);
		/*String str = null ; 
		str.toString();*/
		if(res > 0 )
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
			
			/* 存储data */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", usersAddress.getId() + "");
			dataJSON.put("effect", res + "");
			
			resultJSON.put("data", dataJSON);
		}else
		{
			resultJSON.put("code", "2");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("2"));
		}
		return resultJSON;
	}
	
	public JSONObject updateBatchUsersAddressService(Map<String, Object> condMap)
	{
		JSONObject resultJSON = new JSONObject();
		/* 影响条数 */
		int res = this.usersAddressDao.updateBatch(condMap);
		/*String str = null ; 
		str.toString();*/
		if(res > 0 )
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
			
			/* 存储data */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("effect", res + "");
			
			resultJSON.put("data", dataJSON);
		}else
		{
			resultJSON.put("code", "2");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("2"));
		}
		return resultJSON;
	}
	@Override
	public JSONObject deleteOneUsersAddressService(AUsersAddress usersAddress)
	{
		return null ;
	}
	
	@Override
	public Map<String, Object> findCondListUsersAddressService(PageInfoUtil pageInfoUtil,Map<String, Object> condMap)
	{
		Map<String, Object> resultMap = new HashMap<String, Object>();
		/* 为关键字加上%,实现模糊匹配 
		 * */
		if(condMap.get("keyword") != null && !"".equalsIgnoreCase(condMap.get("keyword") + ""))
		{
			condMap.put("keyword", "%" + condMap.get("keyword")  + "%");
		}
		
		if(pageInfoUtil != null)
		{
			Page page = PageHelper.startPage(pageInfoUtil.getCurrentPage() ,pageInfoUtil.getPageSize());
			List<AUsersAddress> list = this.usersAddressDao.findList(condMap);
			pageInfoUtil.setTotalRecord(Integer.valueOf(page.getTotal() + ""));
			resultMap.put("list", list);
			resultMap.put("totalRecord", pageInfoUtil.getTotalRecord());
			return resultMap;
		}
		/* 不分页查询数据 */
		List<AUsersAddress> list = this.usersAddressDao.findList(condMap);
		resultMap.put("list", list);
		return resultMap;
	}
	
	@Override
	public AUsers findOneUsersService(Map<String, Object> condMap)
	{
		return this.usersDao.findOne(condMap);
	}
	
	@Override
	public JSONObject saveOneUsersService(AUsers users)
	{
		JSONObject resultJSON = new JSONObject();
		/* 影响条数 */
		int res = this.usersDao.save(users);
		/*String str = null ; 
		str.toString();*/
		if(res > 0 )
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
			
			/* 存储data */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", users.getId() + "");
			dataJSON.put("effect", res + "");
			
			resultJSON.put("data", dataJSON);
		}else
		{
			resultJSON.put("code", "1");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("1"));
		}
		return resultJSON;
	}
	
	@Override
	public JSONObject updateOneUsersService(AUsers users)
	{
		JSONObject resultJSON = new JSONObject();
		/* 影响条数 */
		int res = this.usersDao.update(users);
		/*String str = null ; 
		str.toString();*/
		if(res > 0 )
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
			
			/* 存储data */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", users.getId() + "");
			dataJSON.put("effect", res + "");
			
			resultJSON.put("data", dataJSON);
		}else
		{
			resultJSON.put("code", "2");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("2"));
		}
		return resultJSON;
	}

	@Override
	public JSONObject deleteOneUsersService(AUsers users)
	{
		return null ;
	}
	
	@Override
	public List<AUsers> findCondListUsersService(PageInfoUtil pageInfoUtil,Map<String, Object> condMap)
	{
		/* 为关键字加上%,实现模糊匹配 
		 * */
		if(condMap.get("keyword") != null && !"".equalsIgnoreCase(condMap.get("keyword") + ""))
		{
			condMap.put("keyword", "%" + condMap.get("keyword")  + "%");
		}
		
		if(pageInfoUtil != null)
		{
			Page page = PageHelper.startPage(pageInfoUtil.getCurrentPage() ,pageInfoUtil.getPageSize());
			List<AUsers> roleList = this.usersDao.findList(condMap);
			pageInfoUtil.setTotalRecord(Integer.valueOf(page.getTotal() + ""));
			return roleList;
		}
		/* 不分页查询数据 */
		return this.usersDao.findList(condMap);
	}
}
